﻿using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class ProductsBLL
    {
        private ProductsDAL _productsDAL = new ProductsDAL();
        public DataTable SelectProductsCategory()
        {
            //调用DAL
            return _productsDAL.SelectProductsCategory();
        }

        public DataTable SelectProductsInfo()
        {
            //调用DAL
            return _productsDAL.SelectProductsInfo();
        }

        public DataTable SelectProductsInfoByID(int id)
        {
            //调用DAL
            return _productsDAL.SelectProductsInfoByID(id);
        }

        //flag = 0 : 添加; 1 : 修改；2 : 删除
        public int UpdateCategory(ProductsCategory productsCategory, int flag)
        {
            string sql = null;
            SQLiteParameter[] parameters = null;
            switch (flag)
            {
                case 0:
                    sql = "insert into CategoryInfo(CatName, Remark, SubTime) values(@cname, @remark, @stime)";
                    parameters = new SQLiteParameter[]{
                new SQLiteParameter("@cname", productsCategory.CatName),
                new SQLiteParameter("@remark", productsCategory.Remark),
                new SQLiteParameter("@stime", productsCategory.SubTime)
                };

                    
                    return _productsDAL.UpdateCategory(sql, parameters);
                    break;
                case 1:
                    sql = "update CategoryInfo set CatName = @cname, Remark = @remark, SubTime = @stime where Catid = @cid";
                    parameters = new SQLiteParameter[]{
                new SQLiteParameter("@cname", productsCategory.CatName),
                new SQLiteParameter("@remark", productsCategory.Remark),
                new SQLiteParameter("@stime", productsCategory.SubTime),
                new SQLiteParameter("@cid", productsCategory.CatId)
                };
                    return _productsDAL.UpdateCategory(sql, parameters);
                    break;
                case 2:
                    sql = "update CategoryInfo set DelFlag = 1 where Catid = @cid";
                    parameters = new SQLiteParameter[]{
                    new SQLiteParameter("@cid", productsCategory.CatId)
                    };
                    return _productsDAL.UpdateCategory(sql, parameters);
                    break;
                default:
                    return -1;

            }
        }

        public DataTable SeleceProductsInfoByLike(string name)
        {
            return _productsDAL.SeleceProductsInfoByLike(name);
        }

    }
}
